package core.toolbox.formulaAnalysis;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import core.exception.FormulaAnalysisException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/3/13.
 */
public class FormulaAnalysis {

    public static List<Record> analysisString(String formula, String formilaid) throws FormulaAnalysisException {
        Pattern p = Pattern.compile("[\\+\\-＋－]");
        String[] items = p.split(formula);
        Matcher m=p.matcher(formula);
        List<Record> list = new LinkedList();
        boolean first = true;
        for(int i=0;i<items.length;i++){
            String symbol = "+";
            if(!first){
                m.find();
                symbol = m.group();
            }else{
                first = false;
            }
            String item = items[i];
            Pattern p1 = Pattern.compile("[\\u4E00-\\u9FA5]+");
            Matcher m1 = p1.matcher(item);
            m1.find();
            String kmname = null;
            try {
                kmname = m1.group();
            } catch (IllegalStateException e) {
                throw new FormulaAnalysisException("第"+(i+1)+"项 格式错误");
            }
            m1.find();
            String datatype = null;
            try {
                datatype = m1.group();
            } catch (IllegalStateException e) {
                throw new FormulaAnalysisException("第"+(i+1)+"项 ["+kmname+"]没有对应的取数规则");
            }
            if(m1.find()){
                throw new FormulaAnalysisException("第"+(i+1)+"项格式错误 ["+kmname+"]对应的取数规则是["+datatype+"]");
            }

            String kmid = Db.queryStr("select kmid from kjkm where name=?",kmname);

            Record record = new Record();
            record.set("formulaid", formilaid);
            record.set("kmid", kmid);
            record.set("datatype", datatype);
            record.set("symbol", symbol);
            record.set("position", i);
            list.add(record);
        }

        return list;
    }
    //根据每一项的取数规则获取值
    public static BigDecimal getItemValueByDataType(String kmid, String dataType, int period, int userid){
        BigDecimal result = null;
        if("期初余额".equals(dataType)) {
            result = Db.queryBigDecimal("SELECT (IFNULL(jfye,0)-IFNULL(dfye,0)) FROM user_kjkm_period_data WHERE uid = ? AND period = ? AND kmid = ?", userid, period-1, kmid);
        }else if("期末余额".equals(dataType)) {
            result = Db.queryBigDecimal("SELECT (IFNULL(jfye,0)-IFNULL(dfye,0)) FROM user_kjkm_period_data WHERE uid = ? AND period = ? AND kmid = ?", userid, period, kmid);
        }else if("发生额".equals(dataType)) {
            result = Db.queryBigDecimal("SELECT (IFNULL(jfye,0)-IFNULL(dfye,0)) FROM user_kjkm_current_data WHERE uid = ? AND period = ? AND kmid = ?", userid, period, kmid);
        }else{
            throw new RuntimeException("未知的取数规则["+dataType+"]");
        }

        return result==null?new BigDecimal("0"):result;
    }

}
